import { observable, action, runInAction } from 'mobx';
import * as lodash from 'lodash'
import { Http } from '../../services/Http';
import {userInfo, getByIdAsync} from '../../basicData/api';
import { loadavg } from 'os';

namespace common {
  export const http: Http = new Http(); 
}

export class HomeStore {
  @observable public collapsed: boolean;
  @observable public pathname: string;

  @observable public AuthObject: any;

  @observable public messages: any[];
  @observable public messagesNum: number;

  @observable public checkedList: number[];
  @observable public checkAll: boolean;

  @observable public userAuth: string;
  @observable public userId: number;
  @observable public userName: string;
  @observable public companyLogo: string;
  @observable public isAdmin: boolean;
  @observable public showEcode: boolean;  //是否能访问e码平台
  
  public constructor() {
    this.collapsed = false;
    this.pathname = '';
    this.messages = [];
    this.messagesNum = 0;
    this.checkAll = false;
    this.userAuth = '';
    this.userId = null;
    this.isAdmin = false;
    this.showEcode = false;
    this.companyLogo = '';
    this.userName = '';
  }

  @action public changeCollapsed = (bool:boolean) => {
    this.collapsed = bool;
    
  }

  @action public changePathname = (str:string) => {
    this.pathname = str;
  }

  // 获取用户信息
  @action public getUserInfo = () => {
    userInfo().then(
      (res: any) => {
        runInAction( () => {
          this.userAuth = res['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/authorizationdecision']
          this.userId = res['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier']
          let companyId = res['http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid']
          if(this.userAuth.indexOf('Sys.User.Active') !== -1){
            this.isAdmin = true
          } else {
            this.isAdmin = false
          }
          if(this.userAuth.indexOf('Hicap.QRCodeWarehouse') !== -1){
            this.showEcode = true
          } else {
            this.showEcode = false
          }
          if(companyId){
            this.getMyCompany(companyId)
          }
          if(this.userId){
            this.getUserDetail(this.userId)
          }
        })
    })
    
  }

  @action public getMyCompany = (id: number) => {
    getByIdAsync('Company', id).then((res: any) => {
      runInAction( () => {
        this.companyLogo = res.result.companyLogo
      })
    })
  }
  @action public getUserDetail= (id: number) => {
    getByIdAsync('User', id).then((res: any) => {
      runInAction( () => {
        this.userName = res.result.userName
      })
    })
  }

  @action public getMessage = (message: any) => {   
    runInAction(() => {
      let isExit = false
      this.messages.map((item: any) => {
        if(item.messageId === message.messageId) {
          isExit = true
        }
      })
      
      if(!isExit) {
        this.messages.push(message)
        this.messagesNum++
      } else {
        return
      }
    })
  }

  @action public readMessage = (idList: any) => {
    const readnum = idList.length;
    this.messagesNum -= readnum;

    idList.map((id: number) => {
      lodash.remove(this.messages, function(item) {
        return item.messageId === id
      });
    })
  }

  @action public onChange = (e: any) => {
    lodash.forEach(this.messages, function(message){
      if (message.messageId == e.target.value) {
        message.isChecked = e.target.checked
      }
    })

    if(lodash.find(this.messages, ['isChecked', false])){
      this.checkAll = false
    } else {
      this.checkAll = true
    }
    this.getCheckedId();
  }

  @action public onCheckAllChange = (e: any) => {
    this.checkAll = e.target.checked;
    lodash.forEach(this.messages, function(message){
      message.isChecked = e.target.checked
    })
    this.getCheckedId();
  }
  @action public getCheckedId = () => {
    const arr: number[] = [];
    lodash.forEach(this.messages, function(message){
      if(message.isChecked){
        arr.push(message.messageId)
      }
    })
    this.checkedList = arr
  }
}